<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户编号" prop="merchantNo">
        <el-input
          v-model="queryParams.merchantNo"
          placeholder="请输入商户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店编号" prop="storeNo">
        <el-input
          v-model="queryParams.storeNo"
          placeholder="请输入门店编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店中文名" prop="cnName">
        <el-input
          v-model="queryParams.cnName"
          placeholder="请输入门店中文名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入门店简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择门店类型" clearable size="small">
          <el-option
          v-for="dict in mbiStoreTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择门店状态" clearable size="small">
          <el-option
          v-for="dict in mbiStoreStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主门店" prop="mainStore">
        <el-select v-model="queryParams.mainStore" placeholder="请选择主门店" clearable size="small">
          <el-option
          v-for="dict in mbiStoreMainOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="{name:'merchantStoreEdit',params:{type:1}}">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            v-hasPermi="['merchant:store:add']"
          >新增</el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:store:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:store:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户编号" align="center" prop="merchantNo" />
      <el-table-column label="门店编号" align="center" prop="storeNo" />
      <el-table-column label="门店中文名" align="center" prop="cnName" />
      <el-table-column label="门店简称" align="center" prop="shortName" />
      <el-table-column label="门店类型" align="center" prop="type" :formatter="storeTypeFmt" />
      <el-table-column label="门店状态" align="center" prop="status" :formatter="storeStatusFmt" />
      <el-table-column label="主门店" align="center" prop="mainStore" :formatter="mainStoreFmt" />
      <el-table-column label="协议编号" align="center" prop="meanNo" />
      <el-table-column label="门店开始日期" align="center" prop="openStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.openStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店结束日期" align="center" prop="openEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.openEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="buildOperator" />
      <el-table-column label="创建日期" align="center" prop="buildDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.buildDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="{name:'merchantStoreView',params:{type:2,storeNo:scope.row.storeNo}}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['merchant:store:query']"
            >详情</el-button>
          </router-link>
          <router-link :to="{name:'merchantStoreEdit',params:{type:2,storeNo:scope.row.storeNo}}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['merchant:store:edit']"
            >修改</el-button>
          </router-link>
          <router-link :to="{name:'merchantStoreView',params:{type:4,storeNo:scope.row.storeNo}}">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['merchant:store:audit']"
              v-if="scope.row.examineResult == 0 && scope.row.progressBitmap == '11'"
            >审核</el-button>
          </router-link>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:store:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStoreVo, getStore, delStore, addStore, updateStore, exportStore, addViceStore, logicRemoveStoreByStoreNos } from "@/api/merchant/store";
import { listStoreContact, getStoreContact, delStoreContact, addStoreContact, updateStoreContact, exportStoreContact, getStoreContactByStoreNo } from "@/api/merchant/storeContact";
import { listBasicAndOptions } from "@/api/merchant/basic";
import UploadImage from '@/components/UploadImage';
import { getToken } from "@/utils/auth";

export default {
  name: "Store",
  components: {
    UploadImage,
  },
  data() {
    return {
      // 上传的图片服务器地址
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/uploadToLongFileSystem",
      headers: {
        toekn: getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的数组
      storeNos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户门店信息表格数据
      storeList: [],
      //门店类型
      mbiStoreTypeOptions: [],
      //商户列表
      merchantOptions: [],
      //主门店
      mbiStoreMainOptions: [],
      //门店状态
      mbiStoreStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantNo: null,
        storeNo: null,
        cnName: null,
        shortName: null,
        enName: null,
        type: null,
        businessLicense: null,
        showImg1: null,
        showImg2: null,
        showImg3: null,
        showVideo: null,
        status: null,
        mainStore: null,
        meanNo: null,
        openStartDate: null,
        openEndDate: null,
        examineCode: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      //商户门店信息表单校验
      rules: {
        merchantNo: [
          { required: true, message: "商户不能为空", trigger: "blur" }
        ],
        cnName: [
          { required: true, message: "门店中文名不能为空", trigger: "blur" }
        ],
        shortName: [
          { required: true, message: "门店简称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "门店类型不能为空", trigger: "blur" }
        ],
        businessLicense: [
          { required: true, message: "营业执照不能为空", trigger: "blur" }
        ],
        showImg1: [
          { required: true, message: "门店图片1不能为空", trigger: "blur" }
        ],
        showImg2: [
          { required: true, message: "门店图片2不能为空", trigger: "blur" }
        ],
        showImg3: [
          { required: true, message: "门店图片3不能为空", trigger: "blur" }
        ],
        showVideo: [
          { required: true, message: "门店视频不能为空", trigger: "blur" }
        ],
        openStartDate: [
          { required: true, message: "门店开始日期不能为空", trigger: "blur" }
        ],
        openEndDate: [
          { required: true, message: "门店结束日期不能为空", trigger: "blur" }
        ]
      },
      //商户门店联系人信息表单校验
      rulesStoreContact: {
        contactName: [
          { required: true, message: "联系人姓名不能为空", trigger: "blur" }
        ],
        contactMobile: [
          { required: true, message: "联系人手机不能为空", trigger: "blur" }
        ],
        contactTelephone: [
          { required: true, message: "联系人座机不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        post: [
          { required: true, message: "邮编能为空", trigger: "blur" }
        ]
      }
    };
  },
  //每次进入页面时触发
  activated(){
    this.getList();
  },
  created() {
    this.reset();
    this.getList();
    this.getDicts("mct_merchant_store_type").then(response => {
      this.mbiStoreTypeOptions = response.data;
    });
    this.getDicts("mct_merchant_store_main").then(response => {
      this.mbiStoreMainOptions = response.data;
    });
    this.getDicts("mct_merchant_store_status").then(response => {
      this.mbiStoreStatusOptions = response.data;
    });
    listBasicAndOptions().then(response => {
      this.merchantOptions = response.rows;
    });
  },
  methods: {
    /** 查询商户门店信息列表 */
    getList() {
      this.loading = true;
      listStoreVo(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        formStore: {
          id: null,
          merchantNo: null,
          storeNo: null,
          cnName: null,
          shortName: null,
          enName: null,
          type: null,
          businessLicense: null,
          showImg1: null,
          showImg2: null,
          showImg3: null,
          showVideo: null,
          status: null,
          mainStore: "0",
          meanNo: null,
          openStartDate: null,
          openEndDate: null,
          examineCode: null,
          buildOperator: null,
          buildDate: null,
          buildTime: null,
          modifiedOperator: null,
          modifiedDate: null,
          modifiedTime: null
        },
        formStoreContact: {
          id: null,
          merchantNo: null,
          storeNo: null,
          contactName: null,
          contactMobile: null,
          contactTelephone: null,
          address: null,
          post: null,
          examineCode: null,
          buildOperator: null,
          buildDate: null,
          buildTime: null,
          modifiedOperator: null,
          modifiedDate: null,
          modifiedTime: null
        }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.storeNos = selection.map(item => item.storeNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户门店信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStore(id).then(response => {
        this.form.formStore = response.data;
        getStoreContactByStoreNo(this.form.formStore.storeNo).then(response => {
          this.form.formStoreContact = response.data;
          this.open = true;
          this.title = "修改商户门店信息";
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.$refs["formStoreContact"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStore(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              var form = {};
              form.store = this.form.formStore;
              form.storeContact = this.form.formStoreContact;
              addViceStore(form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        })
      });
    },
    /** 门店类型格式化 */
    storeTypeFmt(row, column) {
      var type = row.type;
      if(!type){
        return "";
      }
      var val = type.trim();
      this.mbiStoreTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 门店状态格式化 */
    storeStatusFmt(row, column) {
      var status = row.status;
      if(!status){
        return "";
      }
      var val = status.trim();
      this.mbiStoreStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 主门店格式化 */
    mainStoreFmt(row, column) {
      var mainStore = row.mainStore;
      if(!mainStore){
        return "";
      }
      var val = mainStore.trim();
      this.mbiStoreMainOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeNos = row.storeNo || this.storeNos;
      this.$confirm('是否确认删除商户门店信息编号为"' + storeNos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRemoveStoreByStoreNos(storeNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商户门店信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStore(queryParams);
        }).catch(() => {});
    },
    //文件上传成功
    handleUploadSuccess(res) {
      this.form.formStore.showVideo = res.url;
      this.loading.close();
      this.$refs.showVideo.clearValidate();
    },
    //文件上传之前
    handleBeforeUpload() {
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    //文件上传失败
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
    //文件选择框被修改
    handleChange(file, fileList) {
      // 当多余一个的时候替换文件
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
    },
    /** 日期格式化 */
    dateFmt(str) {
      return str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8);
    }
  }
};
</script>
